wifi.setmode(wifi.STATION)--工作在ap+客服端，下一步可以通过手机设置密码账号
station_cfg={}
station_cfg.ssid="@PHICOMM_DC"  --这里是你的wifi名字
station_cfg.pwd="123321000" --这里是你的wifi密码
wifi.sta.config(station_cfg)

local humidity
local temperature
sda = 3
scl = 4
function init_OLED(sda,scl)
     sla = 0x3c
     i2c.setup(0, sda, scl, i2c.SLOW)
     disp=u8g.ssd1306_128x64_i2c(sla)
     disp:setFont(u8g.font_6x10)
     disp:setFontRefHeightExtendedText()
     disp:setDefaultForegroundColor()
     disp:setFontPosTop()
end
init_OLED(sda,scl)
tmr.alarm(1, 1000, tmr.ALARM_AUTO,
--延时函数等待wifi连接       
   function()
            if wifi.sta.getip()== nil then
                print("not connected, Waiting...")
--未连接显示等待
            else
                tmr.stop(1)
                print("Config done, IP is "..wifi.sta.getip())
                conn=net.createConnection(net.TCP, 0) 
--建立一个连接connection

end
end
)

stat=1

temp=""
city=""
wea=""
lasttime =""

host = "api.seniverse.com" --天气网站

tmr.alarm(3, 1000*100, tmr.ALARM_AUTO,function() --每10秒到网站获取数据
conn=net.createConnection(net.TCP, 0) 
conn:connect(80,"api.thinkpage.cn")
conn:on("receive", function(conn, payload)
s = string.find(payload,'{') 
payload = string.sub(payload,s,-1)

 a = cjson.decode(payload)

 v =  a["results"][1].now
temperature = v.temperature
feels_like = v.feels_like
humidity = v.humidity
visibility = v.visibility
wind_speed = v.wind_speed
wind_scale = v.wind_scale
print_OLED()
end )
conn:on("connection", function(conn, payload)

conn:send("GET  /v3/weather/now.json?key=SdCxrebtW0IXzxMLP&location=jiamusi&language=zh-Hans&unit=c".." HTTP/1.1\r\n".."Host: ".."api.seniverse.com".. "\r\n".."Connection: close\r\n\r\n")
end)
end)

function print_OLED()
    disp:firstPage()
    repeat
     disp:drawFrame(0, 0,126,64)
     disp:drawStr(3, 5, "H:"..humidity)
     disp:drawStr(3, 20,"T:"..temperature)
     
     disp:drawBitmap()
     -- disp:drawStr(0, 40, str1)
     disp:drawFrame(2, 16,126,45)
     until disp:nextPage() == false
end

